package cn.kooki.app.duobao.ui.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.Bean.detail.OtherItemRecord;
import cn.kooki.app.duobao.util.StringUtils;
import cn.kooki.app.duobao.util.TimeUtils;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by xiong on 2015/12/30.
 */
public class OtherWinRecordAdapter extends BaseAdapter {

    private final Context context;
    private ArrayList<OtherItemRecord> datas;

    public interface OnItemClickListener {
        void onitemClick(int type,int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OtherWinRecordAdapter(Context context, ArrayList<OtherItemRecord> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        OtherItemRecord otherItemRecord = datas.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_winrecord_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        int status = StringUtils.nullSafeParseInt(otherItemRecord.status);
        viewHolder.tvNorPeriod.setText("期号: " + otherItemRecord.qishu);
        viewHolder.tvPeriod.setText("期号: " + otherItemRecord.qishu);
        viewHolder.layoutWillreveal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onitemClick(0,position);
                }
            }
        });
        viewHolder.layoutRevealed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onitemClick(0,position);
                }
            }
        });
        viewHolder.layoutUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onitemClick(1,position);
                }
            }
        });
        if (status == 2) {
            viewHolder.layoutWillreveal.setVisibility(View.GONE);
            viewHolder.layoutRevealed.setVisibility(View.VISIBLE);
            viewHolder.layoutUser.setVisibility(View.VISIBLE);
            viewHolder.tvLuckycode.setText(otherItemRecord.q_user_code);
            viewHolder.tvRevealTime.setText("揭晓时间:" + TimeUtils.getTime(otherItemRecord.endtime));
            viewHolder.tvName.setText(otherItemRecord.q_user);
            viewHolder.tvId.setText(otherItemRecord.q_uid + "（唯一不变标识)");
            viewHolder.tvBuytime.setText(otherItemRecord.gounum + "人次");
            Glide.with(context).load(otherItemRecord.q_user_img).into(viewHolder.ivAvatar);
        } else if (status == 1) {
            viewHolder.layoutWillreveal.setVisibility(View.VISIBLE);
            viewHolder.layoutRevealed.setVisibility(View.GONE);
            viewHolder.layoutUser.setVisibility(View.GONE);
        }

        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'layout_winrecord_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.tv_period)
        TextView tvPeriod;
        @Bind(R.id.iv_spt)
        ImageView ivSpt;
        @Bind(R.id.layout_willreveal)
        RelativeLayout layoutWillreveal;
        @Bind(R.id.tv_nor_period)
        TextView tvNorPeriod;
        @Bind(R.id.iv_nor_spt)
        ImageView ivNorSpt;
        @Bind(R.id.tv_revealTime)
        TextView tvRevealTime;
        @Bind(R.id.iv_avatar)
        CircleImageView ivAvatar;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.win_record_winner)
        LinearLayout winRecordWinner;
        @Bind(R.id.tv_id)
        TextView tvId;
        @Bind(R.id.wind_record_userid)
        LinearLayout windRecordUserid;
        @Bind(R.id.tv_luckycode)
        TextView tvLuckycode;
        @Bind(R.id.win_record_lucky_number)
        LinearLayout winRecordLuckyNumber;
        @Bind(R.id.tv_buytime)
        TextView tvBuytime;
        @Bind(R.id.layout_user)
        RelativeLayout layoutUser;
        @Bind(R.id.layout_revealed)
        LinearLayout layoutRevealed;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
